package top.heue.text.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import top.heue.text.R;
import top.heue.text.bean.ContentListBean;
import top.heue.text.util.RunUtil;

public class ContentListAdapter extends BaseAdapter {
    private final LayoutInflater mInflater;
    private final List<ContentListBean> list;
    private final Context context;

    public ContentListAdapter(Context context, List<ContentListBean> list)
    {
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
        this.context = context;
    }


    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public ContentListBean getItem(int position)
    {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub

        ViewHolder holder;

        if (convertView == null)
        {

            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_content, null);
            holder.pinYin = convertView.findViewById(R.id.item_txt_pinYin);
            holder.wenZi = convertView.findViewById(R.id.item_txt_wenZi);
            holder.pinYin.setTextSize(RunUtil.getContentPinYinSize(context));
            holder.wenZi.setTextSize(RunUtil.getContentWenZiSize(context));
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.pinYin.setText(list.get(position).pinYin);
        holder.wenZi.setText(list.get(position).wenZi);
        return convertView;
    }


    public static final class ViewHolder
    {
        public TextView pinYin;
        public TextView wenZi;
    }

    public void add(String q, String a)
    {

        // 告诉系统界面数据发生变化，刷新界面
        notifyDataSetChanged();
    }

    public void delete(int i) {
        list.remove(i);
        notifyDataSetChanged();
    }
}
